package com.wangdao.wechatmall.controller.admin;

import com.wangdao.wechatmall.bean.bo.admin.CategoryCreateBO;
import com.wangdao.wechatmall.bean.pojo.Category;
import com.wangdao.wechatmall.bean.vo.admin.BaseRespVo;
import com.wangdao.wechatmall.bean.vo.admin.CategoryVO;
import com.wangdao.wechatmall.bean.vo.admin.L1DataVO;
import com.wangdao.wechatmall.exception.BrandDescException;
import com.wangdao.wechatmall.exception.BrandNameException;
import com.wangdao.wechatmall.service.admin.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author liyongqiang
 * CreateDate 2021/7/9 21:05
 */

@RestController
@RequestMapping("admin/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    @RequestMapping("list")
    public BaseRespVo queryList(){

        List<CategoryVO> categoryVOS = categoryService.queryList();
        return BaseRespVo.ok(categoryVOS);
    }

    @RequestMapping("l1")
    public BaseRespVo getL1(){

        List<L1DataVO> l1DataVOS = categoryService.getL1();
        return BaseRespVo.ok(l1DataVOS);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody CategoryCreateBO categoryCreateBO) throws BrandDescException, BrandNameException {

        Category category = categoryService.create(categoryCreateBO);
        return BaseRespVo.ok(category);

    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody CategoryVO categoryVO) throws BrandDescException, BrandNameException {

        int update = categoryService.update(categoryVO);
        if (update == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody CategoryVO categoryVO){

        int delete = categoryService.delete(categoryVO);

        if (delete == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }
}
